package com.shinechain.twitter;

import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.RequestToken;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class SignServlet {
    private static final long serialVersionUID = -6205814293093350242L;

    @SuppressWarnings("unused")
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Twitter twitter = new TwitterFactory().getInstance();
        request.getSession().setAttribute("twitter", twitter);

        //OAuth有两种Token，分别是RequestToken和AccessTonke。
        //通过OAuth Consumer的key和secret就可以获取RequestTokenKey和RequestTokenSecret：
        twitter.setOAuthConsumer( "ipp4zV5GKKUncWGmbRAFeV3Yb",  "OZmBI2ZdJaqGMXwE46QI0RnrSUJj7h2WRTMWuENCn2Mm9Hh0OZ");
        RequestToken requestToken = null;
        try {
            requestToken = twitter.getOAuthRequestToken();
            request.getSession().setAttribute("requestToken", requestToken);
            String token = requestToken.getToken();
            String tokenSecret = requestToken.getTokenSecret();
            System.out.println(request.getSession());
            System.out.println(request.getSession().getAttribute("twitter"));
        } catch (TwitterException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



        //跳转到Twitter OAuth验证页面
        response.sendRedirect(requestToken.getAuthorizationURL());

    }

}
